package com.powermanager.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;

import com.powermanager.R;
import com.powermanager.util.ControlUtil;
import com.powermanager.util.SPUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lqy on 16/7/4.
 */
public class UseLineActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener{
    @Bind(R.id.mBtnBack)
    ImageView mBtnBack;
    @Bind(R.id.mBtnSetting)
    ImageView mBtnSetting;
    @Bind(R.id.mToggleBtnZong)
    ToggleButton mToggleBtnZong;
    @Bind(R.id.mToggleBtn1)
    ToggleButton mToggleBtn1;
    @Bind(R.id.mToggleBtn2)
    ToggleButton mToggleBtn2;
    @Bind(R.id.mToggleBtn3)
    ToggleButton mToggleBtn3;
    @Bind(R.id.mToggleBtn4)
    ToggleButton mToggleBtn4;
    @Bind(R.id.mToggleBtn5)
    ToggleButton mToggleBtn5;
    @Bind(R.id.mToggleBtn6)
    ToggleButton mToggleBtn6;
    @Bind(R.id.mToggleBtn7)
    ToggleButton mToggleBtn7;
    @Bind(R.id.mToggleBtn8)
    ToggleButton mToggleBtn8;
    @Bind(R.id.mToggleBtn9)
    ToggleButton mToggleBtn9;
    ControlUtil controlUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_useline);
        ButterKnife.bind(this);
        controlUtil = ControlUtil.getInstance();

        mToggleBtnZong.setChecked(SPUtil.getSharedBooleanData(UseLineActivity.this,"setChecked1",false));
        mToggleBtn1.setChecked(SPUtil.getSharedBooleanData(UseLineActivity.this,"setChecked2",false));
        mToggleBtn2.setChecked(SPUtil.getSharedBooleanData(UseLineActivity.this,"setChecked3",false));
        mToggleBtn3.setChecked(SPUtil.getSharedBooleanData(UseLineActivity.this,"setChecked4",false));
        mToggleBtn4.setChecked(SPUtil.getSharedBooleanData(UseLineActivity.this,"setChecked5",false));
        mToggleBtn5.setChecked(SPUtil.getSharedBooleanData(UseLineActivity.this,"setChecked6",false));
        mToggleBtn6.setChecked(SPUtil.getSharedBooleanData(UseLineActivity.this,"setChecked7",false));
        mToggleBtn7.setChecked(SPUtil.getSharedBooleanData(UseLineActivity.this,"setChecked8",false));
        mToggleBtn8.setChecked(SPUtil.getSharedBooleanData(UseLineActivity.this,"setChecked9",false));
        mToggleBtn9.setChecked(SPUtil.getSharedBooleanData(UseLineActivity.this,"setChecked10",false));

        mToggleBtnZong.setOnCheckedChangeListener(this);
        mToggleBtn1.setOnCheckedChangeListener(this);
        mToggleBtn2.setOnCheckedChangeListener(this);
        mToggleBtn3.setOnCheckedChangeListener(this);
        mToggleBtn4.setOnCheckedChangeListener(this);
        mToggleBtn5.setOnCheckedChangeListener(this);
        mToggleBtn6.setOnCheckedChangeListener(this);
        mToggleBtn7.setOnCheckedChangeListener(this);
        mToggleBtn8.setOnCheckedChangeListener(this);
        mToggleBtn9.setOnCheckedChangeListener(this);
    }

//    @OnClick({R.id.mBtnBack, R.id.mBtnSetting, R.id.mToggleBtnZong, R.id.mToggleBtn1, R.id.mToggleBtn2, R.id.mToggleBtn3, R.id.mToggleBtn4, R.id.mToggleBtn5, R.id.mToggleBtn6, R.id.mToggleBtn7, R.id.mToggleBtn8, R.id.mToggleBtn9})
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.mBtnBack:
//                finish();
//                break;
//            case R.id.mBtnSetting:
//                break;
//            case R.id.mToggleBtnZong:
//                sendData(0x01);
//                break;
//            case R.id.mToggleBtn1:
//                sendData(0x02);
//                break;
//            case R.id.mToggleBtn2:
//                sendData(0x03);
//                break;
//            case R.id.mToggleBtn3:
//                sendData(0x04);
//                break;
//            case R.id.mToggleBtn4:
//                sendData(0x05);
//                break;
//            case R.id.mToggleBtn5:
//                sendData(0x06);
//                break;
//            case R.id.mToggleBtn6:
//                sendData(0x07);
//                break;
//            case R.id.mToggleBtn7:
//                sendData(0x08);
//                break;
//            case R.id.mToggleBtn8:
//                sendData(0x09);
//                break;
//            case R.id.mToggleBtn9:
//                sendData(0x10);
//                break;
//        }
//    }

    //    AA AA 01 00 BB BB CB FE FE 01 12 00 01 01 15 00 FD FD
    public void sendData(int i,boolean isChecked) {
        byte[] bytes = new byte[18];
        bytes[0] = (byte) 0xAA;
        bytes[1] = (byte) 0xAA;
        bytes[2] = 0x01;
        bytes[3] = 0x00;
        bytes[4] = (byte) 0xBB;
        bytes[5] = (byte) 0xBB;
        bytes[6] = (byte) 0xCB;
        bytes[7] = (byte) 0xFE;
        bytes[8] = (byte) 0xFE;
        bytes[9] = (byte) i;
        bytes[10] = 0x12;
        bytes[11] = 0x00;
        bytes[12] = 0x01;
        if (isChecked)
        bytes[13] = 0x01;
        else
        bytes[13] = 0x02;
        int sum = bytes[9] + bytes[10] + bytes[11] + bytes[12] + bytes[13];
        byte[] result = intToByteArray(sum);
        bytes[14] = result[1];
        bytes[15] = result[0];
        bytes[16] = (byte) 0xFD;
        bytes[17] = (byte) 0xFD;
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            String hexValue = Integer.toHexString(v);
            if (hexValue.length() < 2) {
                hexValue = "0"+hexValue;
            }
            Log.d("command--->",hexValue);
        }
        controlUtil.sendData(bytes);
    }
    private static byte[] intToByteArray(int i) {
        byte[] result = new byte[2];
        //由高位到低位
        result[0] = (byte) ((i>> 8) & 0xFF);
        result[1] = (byte) (i & 0xFF);
        return result;
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.mToggleBtnZong:
                sendData(0x01,isChecked);
                SPUtil.setSharedBooleanData(UseLineActivity.this,"setChecked1",isChecked);

                break;
            case R.id.mToggleBtn1:
                sendData(0x02,isChecked);
                SPUtil.setSharedBooleanData(UseLineActivity.this,"setChecked2",isChecked);

                break;
            case R.id.mToggleBtn2:
                sendData(0x03,isChecked);
                SPUtil.setSharedBooleanData(UseLineActivity.this,"setChecked3",isChecked);

                break;
            case R.id.mToggleBtn3:
                sendData(0x04,isChecked);
                SPUtil.setSharedBooleanData(UseLineActivity.this,"setChecked4",isChecked);

                break;
            case R.id.mToggleBtn4:
                sendData(0x05,isChecked);
                SPUtil.setSharedBooleanData(UseLineActivity.this,"setChecked5",isChecked);

                break;
            case R.id.mToggleBtn5:
                sendData(0x06,isChecked);
                SPUtil.setSharedBooleanData(UseLineActivity.this,"setChecked6",isChecked);

                break;
            case R.id.mToggleBtn6:
                sendData(0x07,isChecked);
                SPUtil.setSharedBooleanData(UseLineActivity.this,"setChecked7",isChecked);

                break;
            case R.id.mToggleBtn7:
                sendData(0x08,isChecked);
                SPUtil.setSharedBooleanData(UseLineActivity.this,"setChecked8",isChecked);

                break;
            case R.id.mToggleBtn8:
                sendData(0x09,isChecked);
                SPUtil.setSharedBooleanData(UseLineActivity.this,"setChecked9",isChecked);

                break;
            case R.id.mToggleBtn9:
                sendData(0x10,isChecked);
                SPUtil.setSharedBooleanData(UseLineActivity.this,"setChecked10",isChecked);

                break;
        }
    }
}
